package jvm;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Author: admin
 * @Description:
 * @Date: 2018/8/5 18:18
 */

public class ClassLoad extends ClassLoader {



    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        Singletone singletone = Singletone.singletone;
        System.out.println(singletone.a);
        System.out.println(singletone.b);
        System.out.println(System.getProperty("java.class.path"));

        ClassLoader cl=new ClassLoader() {
            @Override
            public Class<?> loadClass(String name) throws ClassNotFoundException {
                String fileName = name.substring(name.lastIndexOf(".") + 1) + ".class";
                InputStream in=getClass().getResourceAsStream(fileName);
                if(in==null) return super.loadClass(name);
                try {
                    byte[] bytes=new byte[in.available()];
                    in.read(bytes);
                    return defineClass(name,bytes,0,bytes.length);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        };
        String className="jvm.ClassLoad";
        Object a= ClassLoad.class.getClassLoader().loadClass(className).newInstance();
        System.out.println(a instanceof ClassLoad);

        Object b=cl.loadClass(className).newInstance();
        System.out.println(b instanceof ClassLoad);
        String s="hello";
        String ss=new String("hello");
        String sss= "hello";
        System.out.println(s==ss);
        System.out.println(s.equals(ss));
        System.out.println(s==sss);

    }
}

class Singletone {
    public static int a;
    public static int b = 0;
    public static Singletone singletone = new Singletone();

    private Singletone() {
        ++a;
        ++b;
        System.out.println("Singletone a " + singletone.a);
        System.out.println("Singletone b " + singletone.b);
    }
}